#include "map.h"


void drawMap()
 {
    SDL_Rect dest,src;

    dest.x = 0;
    dest.y = 0;

    src.x = map.startX;
    src.y = map.startY;
    src.w = SCREEN_WIDTH;
    src.h = SCREEN_HEIGHT;

    SDL_BlitSurface(map.background, &src, jeu.screen, &dest);
}


void centerScrollingOnPlayer()
 {
    map.startX = player.x - (SCREEN_WIDTH / 2);

    if (map.startX < 0)
    {
        map.startX = 0;
    }
    else if (map.startX + SCREEN_WIDTH >= map.maxX)
    {
        map.startX = map.maxX - SCREEN_WIDTH;
    }
 }


void mapCollision(GameObject *entity)
{
    int x,y;


    x = entity->x + entity->dirX;  

    if(x+entity->w > map.maxX)
    {
        entity->x = map.maxX - entity->w - 1;
    }
    else if (x <= 0)
    {
        entity->x = 0;
    }
    else
    {
        entity->x = x;
    }    

    y = entity->y + entity->dirY + entity->h;

    if(y > 500)
    {
        entity->y = 500 - entity->h ;
        entity->onGround = 1;
    }
    else
    {
        entity->y = y - entity->h;
    }

}


int collide(GameObject *player, GameObject *monster)
{
    if ((player->x >= monster->x + monster->w)
    ||  (player->x + player->w <= monster->x)
    ||  (player->y >= monster->y + monster->h)
    ||  (player->y + player->h <= monster->y)
    )
        return 0;
    
    else if (player->y + player->h <= monster->y + 10)
    {
        player->dirY = -JUMP_HEIGHT;
        return 2;
    }
    //Sinon, on renvoie 1 et c'est le joueur qui meurt...
    else
        return 1;
}